/* App Module */

var ItsmApp = angular.module('ItsmApp',[ 'ionic', 'ui.router', 'appControllers', 'appServices',/*'ngCordova', 'chart.js',*/'angular-input-stars'],
function($httpProvider) {
	$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
});

ItsmApp.run(function($ionicPlatform, $ionicPopup, $rootScope, $state,$location, $ionicHistory, /*$cordovaNetwork,*/ $ionicLoading, $timeout,$http) {

	App.setMyIonicLoading($ionicLoading);
	App.setMyHttp($http);
	App.setMyTimeout($timeout);
	App.setMyState($state);
	App.setMyIonicPopup($ionicPopup);

	App.initType();

	$ionicPlatform.ready(function() {
		if (window.cordova && window.cordova.plugins.Keyboard) {
			cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
		}
		if (window.StatusBar) {
			StatusBar.styleDefault();
		}

		// // 配置企业微信
		// wx.config({
		// 	beta: true, // 必须这么写，否则在微信插件有些jsapi会有问题
		// 	debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
		// 	appId: 'wx2b418a3d21bf8228', // 必填，企业微信的cropID
		// 	timestamp: (new Date()).getTime(), // 必填，生成签名的时间戳
		// 	nonceStr: '', // 必填，生成签名的随机串
		// 	signature: '', // 必填，签名，见附录1
		// 	jsApiList: [] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
		// });

		// 判断网络状态
		document.addEventListener("deviceready", function() {

			// var type = $cordovaNetwork.getNetwork()
			//
			// var isOnline = $cordovaNetwork.isOnline()
			//
			// var isOffline = $cordovaNetwork.isOffline()

			// listen for Online event
			$rootScope.$on('$cordovaNetwork:online', function(event,networkState) {
				var onlineState = networkState;
				//$ionicLoading.hide();
				App.myNotice("网络恢复正常！");
				console.log("device online...");
			})

			// listen for Offline event
			$rootScope.$on('$cordovaNetwork:offline', function(event,networkState) {
				var offlineState = networkState;
				// 提醒用户的网络异常
				//$ionicLoading.show({
				//	template : '网络异常，不能连接到服务器！'
				//});

				App.myNotice("网络异常，不能连接到服务器！",3000);
			})

		}, false);

	});

	$rootScope.$on('$stateChangeStart', function(event, toState, toStateParams,
			fromState, fromStateParams) {

		console.log("Changing state to :");
		console.log(toState);

		if (toState.name.indexOf('login') == -1) {
			var user = Session.loadObject("user");
			console.log("user==" + user);
			if (user == null) {
				console.log("gologin");
				event.preventDefault();
				$state.go("login");
			}
		}

	});
});

ItsmApp.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider/*,ChartJsProvider*/) {
	$ionicConfigProvider.platform.ios.tabs.style('standard');
	$ionicConfigProvider.platform.ios.tabs.position('bottom');
	$ionicConfigProvider.platform.android.tabs.style('standard');
	$ionicConfigProvider.platform.android.tabs.position('standard');

	$ionicConfigProvider.platform.ios.navBar.alignTitle('center');
	$ionicConfigProvider.platform.android.navBar.alignTitle('center');

	$ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
	$ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');

	$ionicConfigProvider.platform.ios.views.transition('ios');
	$ionicConfigProvider.platform.android.views.transition('android');

	$ionicConfigProvider.backButton.icon('ion-ios7-arrow-back');
	$ionicConfigProvider.backButton.text('');
	$ionicConfigProvider.backButton.previousTitleText(false);

	/*ChartJsProvider.setOptions({
      colours: ['#FF5252', '#FF8A80'],
      responsive: false
    });
    // Configure all line charts
    ChartJsProvider.setOptions('Line', {
      datasetFill: false
    });*/


	$stateProvider.state('login', {
		url : '/login',
		templateUrl : 'pages/login.html?v=2',
		controller : 'LoginFormCtrl'
	}).state('fuwuqingqiu-wx', {
		url : '/fuwuqingqiu-wx',
		cache : false,
		templateUrl : 'pages/fuwuqingqiu-wx.html',
		controller : 'FwqqCtrl-wx'
	}).state('fuwuqingqiulist', {
		url : '/fuwuqingqiulist/:comment',
		cache : false,
		templateUrl : 'pages/fuwuqingqiu-list.html',
		controller : 'FwqqListCtrl'
	}).state('fuwuqingqiudetail', {
		url : '/fuwuqingqiudetail/:id',
		cache : false,
		templateUrl : 'pages/fuwuqingqiu-detail.html',
		controller : 'Fwqqdetail'
	}).state('add-comment', {
		url : '/add-comment/:id',
		cache : false,
		controller : 'addCommentCtrl',
		templateUrl : 'pages/add-comment.html'
	}).state('remindList', {
		url : '/remindList/',
		cache : false,
		controller : 'remindListCtrl',
		templateUrl : 'pages/remind-list.html'
	}).state('remindDetail', {
		url : '/remindDetail/:id/:nodeLsh/:flowLsh',
		cache : false,
		controller : 'remindDetailCtrl',
		templateUrl : 'pages/fuwuqingqiu-detail.html'
	}).state('suggestionList', {
		url : '/suggestionList/',
		cache : false,
		controller : 'suggestionListCtrl',
		templateUrl : 'pages/suggestion-list.html'
	});

	$urlRouterProvider.otherwise('/login');
});

ItsmApp.filter('trustHtml', function($sce) {
	return function(input) {
		return $sce.trustAsHtml(input);
	}
});